Color Selection


In [122]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

In [123]:
img = mpimg.imread('testimg.jpg')
print('Type: ', type(img), 'Dimensions: ', img.shape)

xsize = img.shape[1]
ysize = img.shape[0]

# Copy the image before modifying
color_select = np.copy(img)


Type:  <class 'numpy.ndarray'> Dimensions:  (743, 1152, 3)

First, selection criteria is defined


In [124]:
red_lim = 215
green_lim = 215
blue_lim = 215
rgb_threshold = [red_lim, green_lim, blue_lim]

Then, zero out pixels below that threshold


In [125]:
for i in range(0,3):
    thresholds = (img[:,:,0] < rgb_threshold[0]) 
    color_select[thresholds] = [0,0,0]

# Show the new image
plt.imshow(color_select)


Out[125]:
<matplotlib.image.AxesImage at 0x11c5186a0>